//
//  DiscoverViewController.m
//  SinaWebo
//
//  Created by 亦心 on 15/3/7.
//  Copyright (c) 2015年 亦心. All rights reserved.
//

#import "DiscoverViewController.h"
#import "DiscoverService.h"
#import "SearchBar.h"
#import "CardGroup.h"
#import "DiscoverGroup.h"
#import "Card.h"
#import "Picitem.h"
#import "CircleView.h"
#import "SettingCardCell.h"
#import "DiscoverViewCell.h"
#import "LiveListViewController.h"

@interface DiscoverViewController ()
@property (weak, nonatomic) CircleView *circleView;
@property (nonatomic, strong) NSArray *dataArray;
@end

@implementation DiscoverViewController
static NSString *const CELL_ID = @"SettingCardCell";
static NSString *const DIS_CELL_ID = @"DiscoverViewCell";

- (NSArray *)dataArray{
    if (_dataArray == nil) {
        _dataArray = [NSArray array];
    }
    return _dataArray;
}

- (instancetype)initWithStyle:(UITableViewStyle)style{
    return [super initWithStyle:UITableViewStyleGrouped];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.tableView.sectionFooterHeight = 0.0;
    self.tableView.sectionHeaderHeight = 10.0;
    self.tableView.contentInset = UIEdgeInsetsMake(100, 0, 0, 0);
    [self registXib];
    [self addCircleView];
    [self getDiscoverData];
    
}

- (void)addCircleView{
    CircleView *circleView = [CircleView circleView];
    circleView.frame = CGRectMake(0, -100, kScreenW, 125);
    [self.tableView addSubview:circleView];
    self.circleView = circleView;
}

- (void)registXib{
    [self.tableView registerNib:[UINib nibWithNibName:NSStringFromClass([SettingCardCell class]) bundle:nil] forCellReuseIdentifier:CELL_ID];
    
    [self.tableView registerClass:[DiscoverViewCell class] forCellReuseIdentifier:DIS_CELL_ID];
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    Card *card = _dataArray[indexPath.section];
    CardGroup *cGroup = card.card_group[indexPath.row];
    if (cGroup.card_type.integerValue == 17) {
        return 88;
    }
    return 44;
}

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return self.dataArray.count;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0) {
        return 1;
    }
    return [_dataArray[section] card_group].count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    Card *card = _dataArray[indexPath.section];
    CardGroup *cGroup = card.card_group[indexPath.row];
    if (cGroup.card_type.integerValue == 17) {
        DiscoverViewCell *cell = [tableView dequeueReusableCellWithIdentifier:DIS_CELL_ID forIndexPath:indexPath];
        cell.dataArray = [card.card_group[indexPath.row] group];
        return cell;
    }else{
        SettingCardCell *cell = [tableView dequeueReusableCellWithIdentifier:CELL_ID forIndexPath:indexPath];
        
        cell.model = card.card_group[indexPath.row];
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    Card *card = _dataArray[indexPath.section];
    CardGroup *cGroup = card.card_group[indexPath.row];
    if ([cGroup.desc isEqualToString:@"直播"]) {
        LiveListViewController *liveListVC = [[LiveListViewController alloc] init];
        [self.navigationController pushViewController:liveListVC animated:YES];
    }
   
}


- (void)getDiscoverData{
    DiscoverRequest *request = [DiscoverRequest request];
    request.fid = @"1001001";
    request.uicode = @"10000010";
    request.siminfo = @"46001";
    request.containerid = @"1001001";
    [DiscoverService getDiscoverWithRequest:request success:^(DiscoverResponse *response) {
        NSMutableArray *dataArray = [response.cards mutableCopy];
        [self setupHeaderData:dataArray.firstObject];
        [dataArray removeObjectAtIndex:0];
        self.dataArray = dataArray;
        [self.tableView reloadData];
    } error:nil];
}

- (void)setupHeaderData:(Card *)card{
    NSMutableArray *images = [NSMutableArray array];
    NSArray *picItems = [card.card_group.firstObject pic_items];
    if (picItems.count) {
        for (Picitem *picItem in picItems) {
            [images addObject:picItem.pic_big];
        }
    }else if ([card.card_group.firstObject pic]){
        [images addObject:[card.card_group.firstObject pic]];
    }
    
    self.circleView.dataArray = images;
}

@end
